﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.PageFilter;
using ASPNETPerformance.App_BusinessLogic.Utils;

namespace ASPNETPerformance.App_BusinessLogic.HttpModules {
    public class HttpCompressionModule : IHttpModule {
        #region IHttpModule Members

        private static readonly List<string> excludeUrl = new List<string> { "MANAGE.ASPX" };

        void IHttpModule.Dispose() {
        }

        void IHttpModule.Init( HttpApplication context ) {
            context.PreRequestHandlerExecute += new EventHandler( context_PostReleaseRequestState );
        }

        void context_PostReleaseRequestState( object sender, EventArgs e ) {
            this.RegisterCompressFilter();
        }

        private void RegisterCompressFilter() {
            HttpContext context = HttpContext.Current;
            HttpRequest request = context.Request;
            HttpResponse response = HttpContext.Current.Response;
            string url = request.RawUrl.ToUpperInvariant();
            bool isCompression = true;

            excludeUrl.ForEach( f =>
                                   {
                                       isCompression = url.Contains( f ) ? false : true;
                                   } );

            if ( isCompression && context.CurrentHandler is System.Web.UI.Page && context.Request[ "HTTP_X_MICROSOFTAJAX" ] == null &&
                 CommonHelper.GetCookieBool( "IsHttpCompression" ) ) {
                string acceptEncoding = request.Headers[ "Accept-Encoding" ];

                if ( string.IsNullOrEmpty( acceptEncoding ) )
                    return;

                acceptEncoding = acceptEncoding.ToUpperInvariant();

                if ( acceptEncoding.Contains( "GZIP" ) ) {
                    response.AppendHeader( "Content-encoding", "gzip" );
                    response.Filter = new GZipStream( response.Filter, CompressionMode.Compress );
                }
                else if ( acceptEncoding.Contains( "DEFLATE" ) ) {
                    response.AppendHeader( "Content-encoding", "deflate" );
                    response.Filter = new DeflateStream( response.Filter, CompressionMode.Compress );
                }

                if ( CommonHelper.GetCookieBool( "IsViewStateFilter" ) ) {
                    response.Filter = new ViewStateFilterCS( response.Filter );
                }

                if ( CommonHelper.GetCookieBool( "IsNamingContainerFilter" ) ) {
                    response.Filter = new NamingContainerFilterCS( response.Filter );
                }

                if ( CommonHelper.GetCookieBool( "IsWhitespaceFilter" ) ) {
                    response.Filter = new WhitespaceFilterCS( response.Filter );
                }
            }
        }

        #endregion
    }
}